package com.example.ormsqlsplicingframe1.orm.wrapper;

import java.text.SimpleDateFormat;
import java.util.*;

public class Column {

    private List<String> list = new ArrayList<>();

    private Map<String, Object> headers = new HashMap<>();


    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private List<Object> values = null;

    /**
     *
     * @param columnName 列名
     * @param fieldName  字段名
     */
    public Column add(String columnName,String fieldName){
        String a = columnName+" as "+fieldName;
        list.add(a);
        return this;
    }

    /**
     *
     * @param columnName 字段名
     * @param value  值
     * @return
     */
    public Column insertAdd(String columnName,Object value){
        headers.put(columnName,value);
        return this;
    }

    public List<Object> getValues(){
        return values;
    }

    public String getSql(){

        StringBuilder builder = new StringBuilder();

        StringJoiner joiner = new StringJoiner(" , "," ( "," ) ");
        StringJoiner joiner2= new StringJoiner(" , "," ( "," ) ");
        if (list.size()>0){
            int a = 0;
            for (final String s : list) {
                if (a>0){
                    builder.append(" , ");
                }
                builder.append(s);
                a++;
            }
        } else if (headers.size()>0){

            //row 是key
            headers.keySet().forEach(row ->{
                joiner.add(row);
            });
            builder.append(joiner+" VALUES ");
            values = new ArrayList<>();
            headers.keySet().forEach(row ->{
                joiner2.add(" ? ");
                values.add(headers.get(row));
            });
            builder.append(joiner2);
        }else  {
            builder.append(" * ");
        }


        return builder.toString();
    }
}
